package com.youlai.boot.modules.accounts.controller;

import com.youlai.boot.modules.accounts.service.PlatformAccountService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.modules.accounts.model.form.PlatformAccountForm;
import com.youlai.boot.modules.accounts.model.query.PlatformAccountQuery;
import com.youlai.boot.modules.accounts.model.vo.PlatformAccountVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 账户明细前端控制层
 *
 * @author lz0816
 * @since 2025-05-23 15:57
 */
@Tag(name = "账户明细接口")
@RestController
@RequestMapping("/api/v1/platformAccounts")
@RequiredArgsConstructor
public class PlatformAccountController  {

    private final PlatformAccountService platformAccountService;

    @Operation(summary = "账户明细分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('modules.accounts:platformAccount:query')")
    public PageResult<PlatformAccountVO> getPlatformAccountPage(PlatformAccountQuery queryParams ) {
        IPage<PlatformAccountVO> result = platformAccountService.getPlatformAccountPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增账户明细")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('modules.accounts:platformAccount:add')")
    public Result<Void> savePlatformAccount(@RequestBody @Valid PlatformAccountForm formData ) {
        boolean result = platformAccountService.savePlatformAccount(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取账户明细表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('modules.accounts:platformAccount:edit')")
    public Result<PlatformAccountForm> getPlatformAccountForm(
        @Parameter(description = "账户明细ID") @PathVariable Long id
    ) {
        PlatformAccountForm formData = platformAccountService.getPlatformAccountFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改账户明细")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('modules.accounts:platformAccount:edit')")
    public Result<Void> updatePlatformAccount(
            @Parameter(description = "账户明细ID") @PathVariable Long id,
            @RequestBody @Validated PlatformAccountForm formData
    ) {
        boolean result = platformAccountService.updatePlatformAccount(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除账户明细")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('modules.accounts:platformAccount:delete')")
    public Result<Void> deletePlatformAccounts(
        @Parameter(description = "账户明细ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = platformAccountService.deletePlatformAccounts(ids);
        return Result.judge(result);
    }
}
